package com.example.robertluozizhao.framecollectdemo.activity;

import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.robertluozizhao.framecollectdemo.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by robert.luozizhao on 2018/1/27.
 */

public class ScreenDesnityActivity extends AppCompatActivity {


    private static final String TAG = "screen";
    @BindView(R.id.img)
    ImageView img;
    @BindView(R.id.tv_show)
    TextView tvShow;
    private String base;

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen_desnity);
        ButterKnife.bind(this);
        getScreenSizeOfDevice2();

    }

    @OnClick({R.id.img})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img:
                int height = img.getHeight();
                int width = img.getWidth();
                String imgStr = "img width:"+width+"px,height:"+height+"px";
                tvShow.setText(base+"\n\n"+imgStr);
                break;
        }
    }


    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    private void getScreenSizeOfDevice2() {
        Point point = new Point();
        getWindowManager().getDefaultDisplay().getRealSize(point);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        double x = Math.pow(point.x/ dm.xdpi, 2);
        double y = Math.pow(point.y / dm.ydpi, 2);
        double screenInches = Math.sqrt(x + y);
        base = "屏幕分辨率：" + dm.widthPixels + "X" + dm.heightPixels + ";手机尺寸：" + screenInches + ";\n(dpi)px = dip * (dpi / 160)\n屏幕像素密度dpi：" + dm.densityDpi + ";（dpi / 160 ）:" + dm.density ;
        tvShow.setText(base);
        Log.d(TAG, " DisplayMetrics: " + dm+",xdpi:"+dm.xdpi+",ydpi:"+dm.ydpi);
        Log.d(TAG, "Screen inches : " + screenInches);
    }
}
